import Config from 'config.js'
import Util from './util.js'

let http = {
    server(url, params, config) {
        return new Promise((res, rej) => {
            let defaultData = {
                private_key: Config.privateKey,
                is_uniapp: 1,
                // #ifdef H5
                is_h5: 1,
                domain: Config.shareDomain,
                // #endif
                // #ifdef MP
                is_applet: 1,
                // #endif
            }
            let currentData = Object.assign(defaultData, params)
            let header = {
                'X-Requested-With': 'XMLHttpRequest',
            }
            if (uni.getStorageSync('token')) {
                header.Authorization = `${uni.getStorageSync('token')}`
            }
            // 禁止连续点击
            if (config.btn) {
                config.btn.showLoading()
            }
            let success = (result) => {
                if (result.statusCode != 200 && result.statusCode != 201) {
                    rej(result.data)
                    if (config.toast !== false) {
                        Util.showToast({ title: result.data.message })
                    }
                    if(result.statusCode==423){
                        uni.navigateTo({
                             url: '/pages/login/index'
                        });
                       uni.removeStorageSync('token');
                    }
                } else {
                    res(result.data)
                    if (config.toast) {
                        Util.showToast({ title: result.data.message })
                    }
                }
                if (config.btn) {
                    config.btn.hideLoading()
                }
            }
            let ajaxConfig = {
                url: Config.baseUrl + '/api/v1' + url,
                method: config.method,
                data: currentData,
				sslVerify: false,
                header: header,
                dataType: config.dataType || 'json',
                responseType: config.responseType || 'text',
                success,
                fail: (result) => {
                    rej(result.data)
                    if (config.toast !== false) {
                        Util.showToast({ title: result.data.message })
                    }
                   
                },
                complete:()=>{
                    if (config.btn) {
                        config.btn.hideLoading()
                    }
                }
            }

            if (config.method !== 'File') {
                uni.request(ajaxConfig);
            } else {
                // 文件
                ajaxConfig.filePath = params.file;
                delete currentData.file
                delete currentData['Content-Type']
                ajaxConfig.formData = currentData;
                ajaxConfig.name = 'img';
                ajaxConfig.success = (res) => {
                    res.data = JSON.parse(res.data)
                    success(res)
                }
                uni.uploadFile(ajaxConfig);
            }

        })
    },

    $post(url, params, config) {
        return http.server(url, params, Object.assign({ method: 'POST' }, config))
    },
    $get(url, params, config) {
        return http.server(url, params, Object.assign({ method: 'GET' }, config))
    },
    $del(url, params, config) {
        return http.server(url, params, Object.assign({ method: 'DELETE' }, config))
    },
    $put(url, params, config) {
        return http.server(url, params, Object.assign({ method: 'PUT' }, config))
    },
    $uploadFile(url, params, config) {
        return http.server(url, params, Object.assign({ method: 'File' }, config))
    },
    $test(url, params, config) {
        var config = { ...config }
        var params = { ...params }
        return new Promise((res, rej) => {
            if (config.btn) {
                config.btn.showLoading()
            }
            setTimeout(() => {
                res(params)
                if (config.btn) {
                    config.btn.hideLoading()
                }
            }, 1500)
        })
    }
}
export default http

